package com.baizhi.controller;

import com.baizhi.dto.PageDTO;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminService;
import com.baizhi.util.ImageCodeUtil;
import com.baizhi.vo.CommonMessageVO;
import com.baizhi.vo.CommonVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    private static final Logger log = LoggerFactory.getLogger(AdminController.class);

    /**
     * 发送验证码图片
     * @param request
     * @return
     */
    @PostMapping("getImageCodes")
    public String getImageCodes(HttpServletRequest request){
        String imageCode = ImageCodeUtil.getSecurityCode();
        request.getServletContext().setAttribute("code",imageCode);
        log.info("生成验证码为: {}",imageCode);
        String imgBase64 = null;
        try {
            imgBase64 = ImageCodeUtil.careateImgBase64(imageCode);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgBase64;
    }

    /**
     * 登陆
     */
    @PostMapping("login")
    public Map<String,Object> login(@RequestBody Admin admin,String enCode) {
        Map<String, Object> login = adminService.login(admin, enCode);
        return login;
    }

    /**
     * 退出
     */
    @PostMapping("logout")
    public String logout(HttpServletRequest request){
        request.removeAttribute("admin");
        return null;
    }

    /**
     * 查所有
     */
    @PostMapping("queryAllPage")
    public CommonVO queryAllPage(PageDTO pageDTO){
        log.info("管理员接收 页: {}",pageDTO);
        CommonVO commonVO = adminService.pageQueryAll(pageDTO);
        return commonVO;
    }

    /**
     * 修改
     */
    @PostMapping("update")
    public CommonMessageVO update(@RequestBody Admin admin){
        log.info("admin {}",admin);
        try {
            adminService.update(admin);
            System.out.println("admin正确 = " + admin);
            return CommonMessageVO.succeed();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("admin错误 = " + admin);
            return CommonMessageVO.failed();
        }
        //return update;
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    public CommonMessageVO delete(@RequestBody Admin admin){
        log.info("删除 admin {}",admin);
        CommonMessageVO delete = adminService.delete(admin);
        return delete;
    }
    /**
     * 添加管理员
     */
    @PostMapping("add")
    public CommonMessageVO add(@RequestBody Admin admin) throws RuntimeException{
        log.info("admin {}",admin);
        return adminService.add(admin);
    }

    /**
     * 添加前的显示
     */
    @GetMapping("queryById")
    public Admin queryById(Admin admin){
        log.info("admin : {}",admin);
        Long id = admin.getId();
        System.out.println("id = " + id);
        //admin.setId(id);
        Admin admins = adminService.queryById(admin);
        return admins;
    }

}
